package com.as.client.charts;

import java.util.ArrayList;
import java.util.Iterator;

import com.as.client.services.SimpleService;
import com.as.client.services.SimpleServiceAsync;
import com.google.gwt.core.client.GWT;
import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.rpc.ServiceDefTarget;
import com.google.gwt.user.client.ui.ChangeListener;
import com.google.gwt.user.client.ui.ChangeListenerCollection;
import com.google.gwt.user.client.ui.RootPanel;
import com.google.gwt.user.client.ui.Widget;
import com.gwtext.client.data.ArrayReader;
import com.gwtext.client.data.FieldDef;
import com.gwtext.client.data.MemoryProxy;
import com.gwtext.client.data.Record;
import com.gwtext.client.data.RecordDef;
import com.gwtext.client.data.Store;
import com.gwtext.client.data.StringFieldDef;
import com.gwtext.client.widgets.Panel;
import com.gwtext.client.widgets.form.TextField;
import com.gwtext.client.widgets.grid.CellMetadata;
import com.gwtext.client.widgets.grid.ColumnConfig;
import com.gwtext.client.widgets.grid.ColumnModel;
import com.gwtext.client.widgets.grid.EditorGridPanel;
import com.gwtext.client.widgets.grid.Renderer;
import com.gwtext.client.widgets.layout.VerticalLayout;


public class LegendPanel extends Panel implements ChangeListener {
	
	private ChangeListenerCollection _changeListeners;
	ArrayList<String> list = new ArrayList<String>(); 
	
	
	public LegendPanel(){
		
		this.addChangeListener(this);
		
		final SimpleServiceAsync simpleService = (SimpleServiceAsync) GWT.create(SimpleService.class);
		String moduleRelativeURL = GWT.getModuleBaseURL() + "simpleservice";
		((ServiceDefTarget)simpleService).setServiceEntryPoint(moduleRelativeURL);
		
		simpleService.getInterfaceList(new AsyncCallback <ArrayList<String>>(){

			public void onFailure(Throwable arg0) {
				Window.alert("failure interface list");
				
			}

			public void onSuccess(ArrayList<String> arg0) {
				list=arg0; 
				if (_changeListeners != null) {
                    _changeListeners.fireChange(RootPanel.get());
            }
				
				
			}
			
		});

	
		
		 

	}
	
	
//	     private static Object[][] getData() {  
//		         return new Object[][]{ 
//		        		 new Object[]{"eth1", "#8dd5e7"},
//		                 new Object[]{"eth2", "#00b8bf"}
//		                
//		                
//		         };  
//		     } 
	
	private Object[][] createObject (ArrayList<String> l){
		Object[][] o= new Object [l.size()][1];
		int j =0;
		Iterator<String> it = l.iterator(); 
		while(it.hasNext()){
			String id = it.next();
			o[j][0]=id; 
			if(j==0){
				o[j][1]="#00b8bf";
			}
			else{
				o[j][1]="#8dd5e7";
			}
			j++;
			
		}
		 if(o==null){
			 return null;
		 }
		 else{
			 return o;	 
		 }
	}
	
	  public void addChangeListener(ChangeListener listener) {
          if (_changeListeners == null)
                  _changeListeners = new ChangeListenerCollection();
          _changeListeners.add(listener);
  }


	public void onChange(Widget arg0) {
		 MemoryProxy proxy = new MemoryProxy(createObject(list));  
         RecordDef recordDef = new RecordDef(  
                 new FieldDef[]{  
                         new StringFieldDef("interface"),   
                         new StringFieldDef("legend")  
                 }  
         );  
         
         
            ArrayReader reader = new ArrayReader(recordDef);  
                  final Store store = new Store(proxy, reader);  
                  store.sort("interface");
                  store.load();

ColumnConfig inter = new ColumnConfig("interface", "interface", 100, true);  
TextField textField = new TextField();
textField.setSelectOnFocus(true);

ColumnConfig legendConfig = new ColumnConfig("Legend", "legend", 60, true);
     legendConfig.setRenderer(new Renderer() {  
	           public String render(Object value, CellMetadata cellMetadata, Record record, int rowIndex, int colNum, Store store) {  
	                 cellMetadata.setHtmlAttribute("style=\"background:" + value + ";\"");  
	                 return "";  
	             }  
	         });
     
           ColumnModel columnModel = new ColumnModel(new ColumnConfig[]{  
    		                  inter,    
    		                  legendConfig  
    		          });

           
           EditorGridPanel grid = new EditorGridPanel(); 
           grid.setStore(store);
               grid.setColumnModel(columnModel);  
               grid.setWidth(180);
               grid.setVisible(true);
               
               this.add(grid);
      		 this.setAutoHeight(true);
    		 this.setAutoWidth(true);
    		 this.setAutoScroll(true);
    		 this.setVisible(true);
    		 this.doLayout();
    			
    			this.setPaddings(15);
    			this.setLayout(new VerticalLayout(15));
		
	}

}
